#!/usr/bin/env python
# coding=utf-8
# __author__ = 'Yunchao Ling'

from pymongo import MongoClient

client = MongoClient("mongo-node", 27017)
db = client["node"]
collection = db["data"]

infile = open("filelist.tsv", "r")
outfile = open("filelist.out.tsv", "w")
for line in infile:
    line = line.rstrip()
    splitline = line.split("\t")
    result = collection.find_one({"dat_no": splitline[0]}, {"_id": 0, "file_path": 1})
    if result == None:
        outfile.write(line + "\tNo such record.\n")
    elif "file_path" in result:
        outfile.write(line + "\t" + result["file_path"] + "\n")
    else:
        outfile.write(line + "\tNo path.\n")
    outfile.flush()
infile.close()
outfile.close()
client.close()
